package com.jbookstore.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jbookstore.dto.UserDto;
import com.jbookstore.service.UserService;

@Controller
public class UserController extends CommonController {

	static Logger logger = Logger.getLogger(MainController.class);
	
	@Autowired
	private UserService userService;

	@RequestMapping(value = "showSignUp.do", method = RequestMethod.GET)
	public ModelAndView showSignUp() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("/user/signUp");
		return mav;
	}
	
	@RequestMapping(value = "signUp.do", method = RequestMethod.POST)
	public ModelAndView signUp(
			@RequestParam(value="userId", required=false) String userId,
			@RequestParam(value="name", required=false) String name,
			@RequestParam(value="phoneNum", required=false) String phoneNum,
			@RequestParam(value="address", required=false) String address,
			@RequestParam(value="password", required=false) String password,
			@RequestParam(value="repassword", required=false) String repassword, 
			HttpServletRequest req,
			HttpServletResponse res) {
		
		UserDto user = new UserDto();
		user.setUserId(userId);
		user.setName(name);
		user.setPhoneNum(phoneNum);
		user.setAddress(address);
		user.setPassword(password);
		
		ModelAndView mav = new ModelAndView();
		try {
			
			HttpSession session = req.getSession();
			
			userService.signUp(user);
			mav.setViewName("/user/signUpResult");
			session.setAttribute("loginUser", user);
		} 
		
		catch (Exception e) {
			mav = this.errorHandler(e);
		}
		
		return mav;
	}
	
	@RequestMapping(value = "login.do", method = RequestMethod.POST)
	public ModelAndView login (
			@RequestParam(value="userId", required=false) String userId,
			@RequestParam(value = "password", required=false) String password,
			HttpServletRequest req) {
		
		UserDto userDto = new UserDto();
		userDto.setUserId(userId);
		userDto.setPassword(password);
		
		ModelAndView mav = new ModelAndView();
		try {
			
			HttpSession session = req.getSession();
			
			UserDto user = this.userService.login(userDto);	
			mav.setViewName("/user/loginResult");
			session.setAttribute("loginUser", user);
		} 
		
		catch (Exception e) {
			mav = this.errorHandler(e);			
		}
		
		return mav;
	}
		
	@RequestMapping(value = "logout.do", method = RequestMethod.GET)
	public String logout() {
		
		this.getHttpSession().removeAttribute("loginUser");
		return "redirect:main.do";
	}
}
